import 'package:flutter/material.dart';
import 'package:flutter_app_05/action/states/data_manager.dart';
import 'package:flutter_app_05/data/model.dart';
import 'package:flutter_app_05/http/nets.dart';
import 'package:flutter_app_05/utils/MyTheme.dart';
import 'package:flutter_app_05/widget/stateful_widget_extends.dart';
import 'package:flutter_app_05/widget/sdk_extends.dart';

import 'more_dtl.dart';

class More01Page extends StatefulWidget {
  @override
  MorePage01PageState createState() => new MorePage01PageState();
}

class MorePage01PageState extends State<More01Page> {
  List<InfoData> _list;
  ScrollController _scrollController = ScrollController();
  int page=1;
  String loadMoreText = "正在加载中...";
  TextStyle loadMoreTextStyle =
  new TextStyle(color: const Color(0xFF4483f6), fontSize: 14.0);
  var hasData = true;
  @override
  Widget build(BuildContext context) {
    return _list == null
        ? Container(
            child: Center(
              child: Text("加载中..."),
            ),
          )
        :new RefreshIndicator(
      onRefresh: _onRefresh,

      child: ListView.separated(itemBuilder: (context,index){
      return  infoModel(context, _list[index]);
      }, separatorBuilder: (context,index){
        return Divider();
      }, itemCount: _list.length,
      // controller: _scrollController,
      ),
    ) ;

    // ListView(
    //
    //   scrollDirection: Axis.vertical,
    //   children: [
    //     ..._list.map((e) => infoModel(context, e)),
    //   ],
    // )
  }

  Future<void> _onRefresh() async {
    page=1;
    var a = await DataManager.getGetlcb(p: {"page":"1", "perPage": "1"})
        .then((value) {
      _list.clear();

      _list=value.data;
      setState(() {});
    });

  }


  Widget infoModel(BuildContext context, InfoData infoData) {
    return GestureDetector(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            margin: EdgeInsets.only(left: 10.0, top: 10.0),
            height: 100.0,
            width: 100.0,
            decoration: BoxDecoration(
              color: MyTheme(context).getColor('white'),
              border: Border.all(
                color: Colors.transparent,
                width: 2,
              ),
              borderRadius: BorderRadius.circular(12),
            ),
            child: (RequestPaths.urlBase + "/" + infoData.link)
                .getImage(width: 40.0, height: 40.0),
          ),
          SizedBox(
            width: 10.0,
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.only(right: 10.0, top: 10.0),
              height: 100.0,
              decoration: BoxDecoration(
                color: MyTheme(context).getColor('white'),
                border: Border.all(
                  color: Colors.transparent,
                  width: 2,
                ),
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 8.0,
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 10.0),
                    child: Text(
                      infoData.title,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: TextStyle(
                          fontSize: 18.0,
                          color: MyTheme(context).getColor('colorBlack')),
                    ),
                  ),
                  SizedBox(
                    height: 14.0,
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 10.0),
                    child: Text(
                      infoData.introduce,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: TextStyle(fontSize: 12.0, color: Colors.grey),
                    ),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
      onTap: () {
        this.push(MoreDetilPage(
          img: infoData.link,
        ));
      },
    );
  }

  Future<bool> _initInfo() async {
    page=1;
    var a = await DataManager.getGetlcb(p: {"page": "1", "perPage": "1"})
        .then((value) {
      _list = value.data;
      setState(() {});
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _initInfo();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        //已经滑到底了
        if (hasData) {
          //还有数据，加载下一页
          setState(() {
            loadMoreText = "正在加载中...";
            loadMoreTextStyle =
            new TextStyle(color: const Color(0xFF4483f6), fontSize: 14.0);
          });
          page++;
          print("page=" + page.toString());
          getData();
        } else {
          setState(() {
            loadMoreText = "没有更多数据";
            loadMoreTextStyle =
            new TextStyle(color: const Color(0xFF999999), fontSize: 14.0);
          });
        }
      }
    });
  }

  Future getData() async {
    page+=1;
    var a = await DataManager.getGetlcb(p: {"page": page.toString(), "perPage": "1"})
        .then((value) {
      _list.clear();

      _list=value.data;
      if(value.data.length<2){
        hasData=false;
      }
      setState(() {});
    });


  }
}
